<?php
//占
class class_dbpdo
{
	var $conn = null;
//------------------------------------------------------------------------
	function __construct($conn = 0)
	{
		if(is_resource($conn))
		{
			$this->conn = $conn;
			return;
		}
		
		$conn = (int)$conn;
		$this->conn = $GLOBALS['POOL']->get('mysql', $conn);
	}
//------------------------------------------------------------------------
	function query($sql)
	{
		$conn = $this->conn;
			
		if(!$conn)
			return false;
	
		$GLOBALS['LASTSQL'] = $sql;
		$GLOBALS['MYSQL_SQLTYPE'] = db_sqltype($sql);
		
		if($GLOBALS['MYSQL_SQLTYPE'] == 'update' || $GLOBALS['MYSQL_SQLTYPE'] == 'delete')
		{
			$affected = 1;
			$stat = $this->conn->exec($sql);
		}
		else
		{
			$affected = 0;
			$stat = $this->conn->query($sql);
		}
				
		if($stat === false)
		{
			$tmp = $this->conn->errorInfo();
			$GLOBALS['MYSQL_ERR'] = $tmp[2];
			db_stat($GLOBALS['MYSQL_ERR']);
		}
		
		if($affected)
		{
			$GLOBALS['MYSQL_AFFECTED'] = (int)$stat;
		}
				
		return $stat;
	}
//-----------------------------------------------------------------------------
	function findAll($sql)
	{
		$result = $this->query($sql);
		return $result ? $result->fetchAll(PDO::FETCH_ASSOC) : array();
	}
	
//------------------------------------------------------------------------
	function insertID()
	{
		return $this->conn->lastInsertId();
	}
//------------------------------------------------------------------------
	function escape($v)
	{
		return trim($this->conn->quote($v), "'");
	}
//------------------------------------------------------------------------
	function log($msg, $table, $id, $field, $oldv, $newv, $sqlstr, $uname, $cc, $ca, $cf, $time)
	{			
			if($GLOBALS['D_SL'])
			{
				$tmp = "insert into {$GLOBALS['D_SL']} set id=0,uname='{$uname}',cc='{$cc}',ca='{$ca}',cf='{$cf}',sqlstr='".base64_encode($sqlstr)."',msg='{$msg}',tbname='{$table}',rowid='{$id}',fieldname='{$field}', oldv = '{$oldv}', newv = '{$newv}', ctime={$time}";
				$this->conn->exec($tmp);
			}
	}
}